package cn.tedu.v1.v5;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;


/**
 *
 */
public class Client {


    private static final String HOST = "192.168.45.162";
    private static final Integer PORT = 4869;
    private Socket socket;
    private String ip;

    private PrintWriter pw;
    private BufferedReader br;
    private Scanner scanner;

    public Client() {
        try {

            socket = new Socket(HOST, PORT);
            ip = socket.getInetAddress().getHostAddress();
            br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
        } catch (SocketException e) {
            System.out.println("连接被重置或者断开");
        } catch (UnknownHostException e) {
            System.out.println("IP地址有问题，请询问服务器端！");
        } catch (IOException e) {
            System.out.println("客户端主动退出了：断开连接");
        }
    }

    public void start() {
        // System.out.println("~~~~~欢迎使用XX牌QQ聊天室~~~~~");
        Scanner input=new Scanner(System.in);
        //目的：不捕获，确定没有问题，捕获代码具体针对服务器结构
        try {
            //一、接受登录提示
            //1，读入服务器端---输出流
            //服务器端:写“请输入用户名”
            String serverMessage = br.readLine();//服务器端：写“请输入用户名
            System.out.println(serverMessage);//读入账号，必须写出
            //2,写出用户名
            String username=input.nextLine();//输入用户名
            //3，写出用户名，发给服务器
            pw.println(username);//写出给服务器端---获取账号

            //二。接收登陆密码提示
            //1,不如客户端---输出流
            serverMessage=br.readLine();//服务器端:写”请输入密码
            System.out.println(serverMessage);//打印:请输入密码:
            //2，写出密码
            String password=input.nextLine();
            //3,写出密码，发给服务器
            pw.println(password);
            //三,服务器端验证账号跟密码是否正确
            //3.1 读入账号和密码正确的情况
            serverMessage= br.readLine();//登录成功，欢迎进入XX牌聊天室
            System.out.println(serverMessage);//客户端打印:登录成功.
            // 13.2 判断如果是”登录成功，欢迎进入XX牌聊天室”前缀:登录成功
            if(serverMessage.startsWith("登录成功")) {//  true 聊天

                System.out.println("请客户端输入");
                chat();//聊天功能

                while (true) {
                    System.out.println("你：");
                    String message= scanner.nextLine();
                    if ("exit".equalsIgnoreCase(message)){
                        System.out.println("exit");
                        break;
                    }
                }
            }

        }catch(IOException e){
            System.out.println("客户端断开连接~");

        } finally {

            if (pw != null)
                pw.close();
            if (br != null)
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("服务器通讯内容失败~");
                }
            if (socket != null && !socket.isClosed()) {
                System.out.println(ip + "服务端关闭成功！success~");
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println(ip + "客户端关闭失败");
                }
            }
        }

    }
    //1.start  方法---封装  客户端 相互通话方法  chat
    private void chat(){
        new Thread(() -> {
            // System.out.println("~~~~~欢迎使用XX牌QQ聊天室~~~~~");
            try {

                String message;
                while ((message = br.readLine()) != null) {
                    System.out.println("客户端说：" + message);
                    System.out.println("服务器端请输入：");
                }
            } catch (IOException e) {
                System.out.println("客户端发来的消息断开了~");
            }
        }).start();


        System.out.println("客户端请输入：");
        Scanner input = new Scanner(System.in);
        while (true) {
            String say = input.nextLine();
            pw.println(say);


            if (say.equalsIgnoreCase("exit")) {
                System.out.println("退出啦");
                break;


            }
        }


    }
    public static void main(String[] args) {
        new Client().start();
    }

}
